<?php

/**
* AdminSendCodeTest.php
*
* Test Code Send
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminSendCodeTest extends WebTestCase
{
    private $url;
    private $testNAme = array();
    private $idArray = array();
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@zieba.info";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url =$global['service_url'];
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");   
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[0]."','".$this->username."' )");
        $this->idArray[$this->testName[0]] = $dbh->lastInsertId();

        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."' )");
        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."' )");
        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."' )");

        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[1]."','".$this->username."' )");
        $this->idArray[$this->testName[1]] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[2]."','".$this->username."' )");
        $this->idArray[$this->testName[2]] = $dbh->lastInsertId();
        $this->get($this->url.'?cmd=AdminGuideList');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

        $this->get($this->url.'?cmd=AdminShowRoute&id='.$this->idArray[$this->testName[0]] );
    }

    public function testSendCode()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertLink('[Wyślij kody]',$this->url."index.php?cmd=AdminSendCode&id=".$this->idArray[$this->testName[0]]);
        $this->click('[Wyślij kody]',$this->url."index.php?cmd=AdminSendCode&id=".$this->idArray[$this->testName[0]]);
        $this->assertText('Kody wysłane.');
    }


    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("DELETE FROM slide WHERE guide_id = ".$this->idArray[$this->testName[0]]);
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[0]."'" );
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[1]."'" );
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[2]."'" );

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
        $dbh = null;
    }
    
}
?>
